import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;

class LeetCode622<E> implements Queue<E>, Iterable<E> {

    private class Node {
        E value;
        Node next;


        public Node(E value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    //头节点
    private Node head = new Node(null, null);
    //尾节点
    private Node tail = head;
    //节点数
    private int size = 0;
    //容量
    private int capacity = Integer.MAX_VALUE;


    public LeetCode622(int capacity) {
        this.capacity = capacity;
        tail.next = head;
    }

    //添加元素到尾部
    @Override
    public boolean offer(E e) {
        //队列满了
        if (isFull()) {
            return false;
        }
        Node add = new Node(e, head);
        tail.next = add;
        tail = add;
        size++;
        return true;
    }

    public Boolean isFull() {
        return size == capacity;
    }

    @Override
    public boolean add(E e) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public E remove() {
        return null;
    }

    //获取头部 并移除
    @Override
    public E poll() {
        if (isEmpty()) {
            return null;
        }
        head.next = head.next.next;
        if (head.next == tail) {
            tail = head;
        }
        size--;
        return head.next.value;
    }

    @Override
    public E element() {
        return null;
    }

    //获取头部 不移除
    @Override
    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return head.next.value;
    }

    @Override
    public int size() {
        return 0;
    }

    //判断队列是否为空
    @Override
    public boolean isEmpty() {
        return head == tail;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    //迭代器
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node p = head.next;

            //这是什么时候跳出
            @Override
            public boolean hasNext() {
                return p != head;
            }

            //保存值 并后移一位
            @Override
            public E next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }
}

